﻿namespace Pub.Class
{
    using System;
    using System.IO;
    using System.Text;
    using System.Web.UI;

    public class PageBase : Page
    {
        protected StringBuilder css = new StringBuilder();
        protected string description = string.Empty;
        protected int index;
        protected StringBuilder js = new StringBuilder();
        protected string keywords = string.Empty;
        protected string lang = string.Empty;
        private static readonly ISafeDictionary<string, ISafeDictionary<string, string>> langList = new SafeDictionarySlim<string, ISafeDictionary<string, string>>();
        public string RootPath = Request2.GetRelativeRoot();
        protected string skin = "default";
        protected string title = string.Empty;

        private ISafeDictionary<string, string> GetLang()
        {
            if (this.lang.IsNullEmpty())
            {
                Msg.WriteEnd("语言未设置！");
            }
            string filename = "".GetMapPath() + @"\lang\{0}.lang".FormatWith(this.lang);
            if (!FileDirectory.FileExists(filename))
            {
                Msg.WriteEnd("语言文件{0}.lang不存在！".FormatWith(this.lang));
            }
            string str = string.Empty;
            ISafeDictionary<string, string> dictionary = new SafeDictionarySlim<string, string>();
            using (StreamReader reader = new StreamReader(filename, Encoding.UTF8))
            {
                while ((str = reader.ReadLine()).IsNotNull())
                {
                    int index = str.IndexOf('=');
                    if (!str.IsNullEmpty() && (index != -1))
                    {
                        string key = str.Substring(0, index).Trim();
                        string str4 = str.Substring(index + 1).Trim();
                        if (!dictionary.ContainsKey(key))
                        {
                            dictionary.Add(key, str4);
                        }
                        else
                        {
                            dictionary[key] = str4;
                        }
                    }
                }
            }
            return dictionary;
        }

        public string GetLang(string key)
        {
            if (!langList.ContainsKey(this.lang))
            {
                langList[this.lang] = this.GetLang();
            }
            if (!langList[this.lang].ContainsKey(key))
            {
                return string.Empty;
            }
            return langList[this.lang][key];
        }

        public string CSS
        {
            get
            {
                return this.css.ToString();
            }
            set
            {
                value.Split(new char[] { ';' }).Do<string>((Action<string, int>) ((s, i) => this.js.AppendFormat("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />", s)));
            }
        }

        public string Description
        {
            get
            {
                return this.description;
            }
            set
            {
                this.description = (value.Length > 0) ? (value + ", ") : value;
            }
        }

        public int Index
        {
            get
            {
                return this.index;
            }
            set
            {
                this.index = value;
            }
        }

        public string JS
        {
            get
            {
                return this.js.ToString();
            }
            set
            {
                value.Split(new char[] { ';' }).Do<string>((Action<string, int>) ((s, i) => this.js.AppendFormat("<script language=\"javascript\" type=\"text/javascript\" src=\"{0}\"></script>", s)));
            }
        }

        public string Keywords
        {
            get
            {
                return this.keywords;
            }
            set
            {
                this.keywords = (value.Length > 0) ? (value + ", ") : value;
            }
        }

        public string Lang
        {
            get
            {
                return this.lang;
            }
            set
            {
                this.lang = value;
            }
        }

        public string Skin
        {
            get
            {
                return this.skin;
            }
            set
            {
                this.skin = value;
            }
        }

        public string Title
        {
            get
            {
                return this.title;
            }
            set
            {
                this.description = value + ", ";
                this.keywords = value + ", ";
                this.title = value + " - ";
            }
        }
    }
}

